package com.saop;

import com.alibaba.fastjson.JSONObject;
import com.tools.HttpClientUtil;

import java.util.HashMap;

public class SoAndJLFeedback {


    static  String custOrderNbr = "";
    static  String extCustOrderId = "2312001020313576";


    public static void main(String[] args) {
        int state =1;
        String custOrderId ="";
        if (!extCustOrderId.isEmpty()) {
            state=2;
            String sql = "select ol_id from message_order where ext_cust_order_id = ?";
            custOrderId  = new SaopDatabase().getSaopDatabase(sql, extCustOrderId, 2);


        }

        if(!custOrderNbr.isEmpty()) {
            String getCustOrderIdSql = "SELECT Cust_order_id from  customer_order a where a.CUST_ORDER_nbr  = ?";

            custOrderId = new Sodatabase().getSoDatabase(getCustOrderIdSql, custOrderNbr);
        }

        if (custOrderId != null || state ==2) {
            String getOrderItemIdsSql = "SELECT  ORDER_ITEM_ID   from order_item a where a.CUST_ORDER_ID = ?";
            String orderItemIds = new Sodatabase().getSoDatabase(getOrderItemIdsSql,custOrderId);
            if (SendSoFeedBack(orderItemIds) == 0) {

                String getIslaeSql = "SELECT provisale from  customer_order_group_rel a where a.CUST_ORDER_ID =?";
                String isale = new Sodatabase().getSoDatabase(getIslaeSql,custOrderId);

                if (isale != null) {
                    String sql = "select OL_ID,lan_id,ext_cust_order_id from MESSAGE_ORDER a where a.ol_id = ?";
                    String saopDatabase = new SaopDatabase().getSaopDatabase(sql, custOrderId, 1);

                    if (saopDatabase != null) {
                        System.out.println("等待十秒调用集团报竣-start");
                        try {
                            Thread.sleep(10000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("等待十秒调用集团报竣-end");

                        sendSaopFeedBack(saopDatabase);
                    }
                }

            } else
                System.out.println("受理归档失败");
        } else
            System.out.println("无此订单");


    }


    static int SendSoFeedBack(String orderItemIds) {

        String url = "http://136.192.50.109:60013/so-service/service/so_order_dealOssFeedbackMsg";
        String json = "[\n" +
                "{\n" +
                "      \"orderItemIds\" : [" +
                orderItemIds +
                "      ],\n" +
                "      \"msgType\" : \"1000\",\n" +
                "      \"msg\" : \"订单项施工完成消息_post\",\n" +
                "      \"completeDate\" : \"2018-11-05 10:47:37\"\n" +
                "    }\n" +
                "]";

        String res = HttpClientUtil.doPost(url, json, "UTF-8", 30000, new HashMap<String, String>());
        System.out.println("调用受理归档接口-" + res);
        JSONObject jsonObject = JSONObject.parseObject(res);
        String resultCode = jsonObject.getString("resultCode");
        if ("0".equals(resultCode)) {
            return 0;
        }
        return 1;
    }

    private static void sendSaopFeedBack(String req) {
        String[] split = req.split(",");
        String json = "{\n" +
                "    \"contractRoot\": {\n" +
                "        \"svcCont\": {\n" +
                "            \"feedbackReq\": {\n" +
                "                \"coId\": \"" + split[0] + "\",\n" +
                "                \"lanId\": \"" + split[1] + "\"\n" +
                "            }\n" +
                "        },\n" +
                "        \"tcpCont\": {\n" +
                "            \"appKey\": \"SF00000017\",\n" +
                "            \"dstSysId\": \"SF00000000\",\n" +
                "            \"reqTime\": \"2018-07-20 10:41:30\",\n" +
                "            \"sign\": \"***\",\n" +
                "            \"svcCode\": \"usb.orderFeedback\",\n" +
                "            \"transactionId\": \"JL00000018201807202054490289\",\n" +
                "            \"version\": \"V1.0\"\n" +
                "        }\n" +
                "    }\n" +
                "}";
        String url = "http://136.192.50.109:18450/saop-service/service/saop_exchange";

        String res = HttpClientUtil.doPost(url, json, "UTF-8", 300000, new HashMap<String, String>());
        System.out.println("调用集团归档-" + res);
        System.out.println("ext_cust_order_id:" + split[2]);

    }


}
